package com.ytzl.itrip.controller;

import com.ytzl.itrip.common.DtoUtil;
import com.ytzl.itrip.common.EmptyUtils;
import com.ytzl.itrip.common.ErrorCode;
import com.ytzl.itrip.dto.Dto;
import com.ytzl.itrip.pojo.ItripUser;
import com.ytzl.itrip.pojo.ItripUserLinkUser;
import com.ytzl.itrip.vo.user.ItripModifyUserLinkUserVO;
import com.ytzl.itrip.vo.user.ItripSaveUserLinkUserVO;
import com.ytzl.itrip.vo.user.ItripSearchUserLinkUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by sam on 2018/2/9.
 */
@Controller
@Api(tags = "常用联系人模块业务")
@RequestMapping("/api/userinfo")
public class ItripUserLinkUserController extends BaseController {


    @ApiOperation(value = "查询常用联系人接口", httpMethod = "POST", produces = "application/json", response = Dto.class,
            notes = "查询常用联系人接口(可根据联系人名称模糊查询)" +
                    "<p>若查询所有联系人，可不传递参数|若根据联系人姓名模糊查询，如果传入参数</p>" +
                    "<p>成功返回success=true 失败 success=false</p>，失败错误码如下:" +
                    "<p>错误码:</p>" +
                    "<p>100401:获取常用联系人信息失败</p>" +
                    "<p>100000:Token失效</p>")
    @RequestMapping(value = "queryuserlinkuser", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Dto queryuserlinkuser(@RequestBody ItripSearchUserLinkUserVO itripSearchUserLinkUserVO, HttpServletRequest request) {
        //查询条件，当前登录用户ID && 常用联系人名称
        //  已经拥有条件: 常用联系人名称
        //  需要准备条件:当前登录用户ID
        //因为redis中token保存的值就是当前的登录信息，获取代码如下
        String token = request.getHeader("token");
        ItripUser itripUser = validationToken.getCurrentUser(token);
        if (itripUser == null)
            return DtoUtil.returnFail("Token失效", ErrorCode.BIZ_TOKEN_INVALID);
        String linkUserName = (null == itripSearchUserLinkUserVO) ? null : itripSearchUserLinkUserVO.getLinkUserName();
        //查询
        Map<String, Object> param = new HashMap();
        param.put("userId", itripUser.getId());
        param.put("linkUserName", linkUserName);
        //调用查询
        try {
            List<ItripUserLinkUser> itripUserLinkUsers = itripUserLinkUserService.getItripUserLinkUserListByMap(param);
            return DtoUtil.returnSuccess("获取常用联系人成功", itripUserLinkUsers);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("获取常用联系人失败", ErrorCode.BIZ_QUERY_LINK_USER_FAILED);
        }
    }

    @ApiOperation(value = "新增常用联系人接口", httpMethod = "POST",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "新增常用联系人信息" +
            "<p>成功：success = ‘true’ | 失败：success = ‘false’ 并返回错误码，如下：</p>" +
            "<p>错误码：</p>" +
            "<p>100411 : 新增常用联系人失败 </p>" +
            "<p>100412 : 不能提交空，请填写常用联系人信息</p>" +
            "<p>100000 : token失效，请重登录 </p>")
    @RequestMapping(value = "/adduserlinkuser", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Dto adduserlinkuser(@RequestBody ItripSaveUserLinkUserVO itripSaveUserLinkUserVO, HttpServletRequest request) {
        //获取当前登录用户
        String token = request.getHeader("token");
        ItripUser itripUser = validationToken.getCurrentUser(token);
        if (itripUser != null && itripSaveUserLinkUserVO != null) {
            //构建对象
            ItripUserLinkUser itripUserLinkUser = new ItripUserLinkUser();
            itripUserLinkUser.setLinkIdCard(itripSaveUserLinkUserVO.getLinkIdCard());
            itripUserLinkUser.setLinkIdCardType(itripSaveUserLinkUserVO.getLinkIdCardType());
            itripUserLinkUser.setLinkPhone(itripSaveUserLinkUserVO.getLinkPhone());
            itripUserLinkUser.setLinkUserName(itripSaveUserLinkUserVO.getLinkUserName());
            itripUserLinkUser.setUserId(itripUser.getId());
            itripUserLinkUser.setCreatedBy(itripUser.getId());
            itripUserLinkUser.setCreationDate(new Date());
            //保存数据
            try {
                itripUserLinkUserService.saveItripUserLinkUser(itripUserLinkUser);
            } catch (Exception e) {
                e.printStackTrace();
                return DtoUtil.returnFail("添加常用联系人失败", ErrorCode.BIZ_SAVE_NEW_USER_LINK_USER_FAILED);
            }
            return DtoUtil.returnSuccess("新添常用联系人成功");
        } else if (itripUser != null && itripSaveUserLinkUserVO == null)
            return DtoUtil.returnFail("请提交数据", ErrorCode.BIZ_SAVE_NEW_USER_LINK_USER_IS_NOT_NULL);
        else
            return DtoUtil.returnFail("TOKEN失效", ErrorCode.BIZ_TOKEN_INVALID);
    }


    @ApiOperation(value = "修改常用联系人接口", httpMethod = "POST",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "修改常用联系人信息" +
            "<p>成功：success = ‘true’ | 失败：success = ‘false’ 并返回错误码，如下：</p>" +
            "<p>错误码：</p>" +
            "<p>100421 : 修改常用联系人失败 </p>" +
            "<p>100422 : 不能提交空，请填写常用联系人信息</p>" +
            "<p>100000 : token失效，请重登录 </p>")
    @RequestMapping(value = "/modifyuserlinkuser", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Dto modifyuserlinkuser(@RequestBody ItripModifyUserLinkUserVO itripModifyUserLinkUserVO, HttpServletRequest request) {
        //获取当前登录用户
        String token = request.getHeader("token");
        ItripUser itripUser = validationToken.getCurrentUser(token);
        if (itripUser != null && itripModifyUserLinkUserVO != null) {
            //构建对象
            ItripUserLinkUser itripUserLinkUser = new ItripUserLinkUser();
            itripUserLinkUser.setLinkIdCard(itripModifyUserLinkUserVO.getLinkIdCard());
            itripUserLinkUser.setLinkIdCardType(itripModifyUserLinkUserVO.getLinkIdCardType());
            itripUserLinkUser.setLinkPhone(itripModifyUserLinkUserVO.getLinkPhone());
            itripUserLinkUser.setLinkUserName(itripModifyUserLinkUserVO.getLinkUserName());
            itripUserLinkUser.setId(itripModifyUserLinkUserVO.getId());
            itripUserLinkUser.setModifiedBy(itripUser.getId());
            itripUserLinkUser.setModifyDate(new Date());
            //保存数据
            try {
                itripUserLinkUserService.modifyItripUserLinkUser(itripUserLinkUser);
            } catch (Exception e) {
                e.printStackTrace();
                return DtoUtil.returnFail("修改常用联系人失败", ErrorCode.BIZ_MODIFY_USER_LINK_USER_FAILED);
            }
            return DtoUtil.returnSuccess("修改常用联系人成功");
        } else if (itripUser != null && itripModifyUserLinkUserVO == null)
            return DtoUtil.returnFail("请提交数据", ErrorCode.BIZ_MODIFY_USER_LINK_USER_IS_NOT_NULL);
        else
            return DtoUtil.returnFail("TOKEN失效", ErrorCode.BIZ_TOKEN_INVALID);
    }

    @ApiOperation(value = "删除常用联系人接口", httpMethod = "GET",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "删除常用联系人信息" +
            "<p>成功：success = ‘true’ | 失败：success = ‘false’ 并返回错误码，如下：</p>" +
            "<p>错误码：</p>" +
            "<p>100431 : 所选的常用联系人中有与某条待支付的订单关联的项，无法删除 </p>" +
            "<p>100432 : 删除常用联系人失败 </p>" +
            "<p>100433 : 请选择要删除的常用联系人</p>" +
            "<p>100000 : token失效，请重登录 </p>")
    @RequestMapping(value = "/deluserlinkuser", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Dto deluserlinkuser(Long[] ids, HttpServletRequest request) {
        //获取当前登录用户
        String token = request.getHeader("token");
        ItripUser itripUser = validationToken.getCurrentUser(token);
        List<Long> idsList = new ArrayList<Long>();
        if (itripUser != null && EmptyUtils.isNotEmpty(ids)) {
            try {
                //删除数据
                List<Long> linkUserIds = itripOrderLinkUserService.getItripOrderLinkUserIdsByOrder();
                //将ids数组中的数据添加到idsList集合中
                Collections.addAll(idsList, ids);
                //保留另一个集合所包含的所有内容
                idsList.retainAll(linkUserIds);
                if (idsList.size() > 0) {
                    return DtoUtil.returnFail("所选的常用联系人中有与某条待支付的订单关联的项，无法删除", ErrorCode.BIZ_REMOVE_CONTACT_IS_BINDING);
                } else {
                    itripUserLinkUserService.removeItripUserLinkUserByIds(ids);
                }
            } catch (Exception e) {
                e.printStackTrace();
                return DtoUtil.returnFail("删除常用联系人失败", ErrorCode.BIZ_REMOVE_USER_LINK_USER_FAILED);
            }
            return DtoUtil.returnSuccess("修改常用联系人成功");
        } else if (itripUser != null && EmptyUtils.isEmpty(ids))
            return DtoUtil.returnFail("请选择要删除的用户", ErrorCode.BIZ_PLEASE_SELECT_REMOVE_CONTACT);
        else
            return DtoUtil.returnFail("TOKEN失效", ErrorCode.BIZ_TOKEN_INVALID);
    }
}
